package com.hardydou.pattern.singleton;


import java.util.concurrent.atomic.AtomicInteger;

/**
 * 单例模式
 * <p>
 * 饿汉式实现
 *
 * @author hardy
 * @date
 */
public class SimpleSingleton {

    private static AtomicInteger count = new AtomicInteger(0);
    private static boolean initOk = false;


    /**
     * 2、创建实例对象
     */
    private static SimpleSingleton instance = new SimpleSingleton();



    /**
     * 1、私有化构造、收回创建权限
     */
    private SimpleSingleton() {
        //创建次数统计
        count.getAndAdd(1);
        /**
         * 增肌创建耗时
         */
        try {
            Thread.sleep(500L);
        } catch (InterruptedException e) {
        }
        initOk = true;
    }

    /**
     * 3、统一对象获取方式
     *
     * @return
     */
    public static SimpleSingleton getInstance() {
        return instance;
    }


    /**
     * 测试用
     *
     * @return
     */
    public static int getCount() {
        return count.get();
    }

    public static boolean hasInit() {
        return initOk;
    }
}
